package com.ruoyi.tcod.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.tcod.domain.bo.TcImgOrderApproveBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tcod.domain.vo.TcImgOrderVo;
import com.ruoyi.tcod.domain.bo.TcImgOrderBo;
import com.ruoyi.tcod.service.ITcImgOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 单子
 *
 * @author ruoyi
 * @date 2024-10-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tcod/imgOrder")
public class TcImgOrderController extends BaseController {

    private final ITcImgOrderService iTcImgOrderService;

    /**
     * 查询单子列表
     */
    @SaCheckPermission("tcod:imgOrder:list")
    @GetMapping("/list")
    public TableDataInfo<TcImgOrderVo> list(TcImgOrderBo bo, PageQuery pageQuery) {
        return iTcImgOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单子列表
     */
    @SaCheckPermission("tcod:imgOrder:export")
    @Log(title = "单子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TcImgOrderBo bo, HttpServletResponse response) {
        List<TcImgOrderVo> list = iTcImgOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "单子", TcImgOrderVo.class, response);
    }

    /**
     * 获取单子详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tcod:imgOrder:query")
    @GetMapping("/{id}")
    public R<TcImgOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTcImgOrderService.queryById(id));
    }

    /**
     * 新增单子
     */
    @SaCheckPermission("tcod:imgOrder:add")
    @Log(title = "单子", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TcImgOrderBo bo) {
        return toAjax(iTcImgOrderService.insertByBo(bo));
    }

    /**
     * 修改单子
     */
    @SaCheckPermission("tcod:imgOrder:edit")
    @Log(title = "单子", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TcImgOrderBo bo) {
        return toAjax(iTcImgOrderService.updateByBo(bo));
    }

    /**
     * 删除单子
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tcod:imgOrder:remove")
    @Log(title = "单子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTcImgOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaCheckPermission("tcod:imgOrder:approve")
    @Log(title = "单子审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/approve")
    public R<Void> approve(@Validated(AddGroup.class) @RequestBody TcImgOrderApproveBo bo) {
        iTcImgOrderService.approve(bo);
        return R.ok();
    }
}
